<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/6
 * Time: 17:22
 */

namespace backend\models\c2\entity;


use common\models\c2\entity\Product;
use common\models\c2\entity\ProductCombination;
use common\models\c2\entity\ProductPackage;
use common\models\c2\entity\WarehouseCommitDeliveryItem;
use common\models\c2\entity\WarehouseCommitNote;
use common\models\c2\entity\WarehouseCommitReceiptItem;
use common\models\c2\statics\WarehouseCommitNoteType;
use common\models\c2\statics\WarehouseCommitState;

class WarehouseCommitDeliveryNote extends WarehouseCommitNote
{

    public function loadItems()
    {
        $this->items = $this->getCommitDeliveryItems()->all();
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if (!empty($this->items)) {
            foreach ($this->items as $item) {
                $product = Product::findOne($item['product_id']);
                $combination = ProductCombination::findOne($item['combination_id']);
                $package = ProductPackage::findOne($item['package_id']);
                $pieces = isset($item['pieces']) ? $item['pieces'] : 0;
                $product_sum = $item['product_sum'] == '' ? $package->product_capacity * $pieces : $item['product_sum'];
                $attributes = [
                    'label' => isset($item['label']) ? $item['label'] : '',
                    'type' => WarehouseCommitNoteType::TYPE_DELIVERY,
                    'note_id' => $this->note_id,
                    'product_id' => isset($item['product_id']) ? $item['product_id'] : 0,
                    'product_name' => !is_null($product) ? $product->name : '',
                    'product_sku' => !is_null($product) ? $product->sku : '',
                    // 'product_label' => !is_null($product) ? $product->label : '',
                    // 'product_value' => !is_null($product) ? $product->value : '',
                    'combination_id' => !is_null($combination) ? $combination->id : 0,
                    'combination_name' => !is_null($combination) ? $combination->name : '',
                    'package_id' => !is_null($package) ? $package->id : 0,
                    'package_name' => !is_null($package) ? $package->name : '',
                    'measure_id' => isset($item['measure_id']) ? $item['measure_id'] : 0,
                    'pieces' => $pieces,
                    'product_sum' => $product_sum,
                    'produce_number' => $item['produce_number'] == '' ? 0 : $item['produce_number'],
                    'stock_number' => $item['stock_number'] == '' ? 0 : $item['stock_number'],
                    'memo' => isset($item['memo']) ? $item['memo'] : '',
                    'position' => isset($item['position']) ? $item['position'] : 0,
                ];
                if (isset($item['id']) && $item['id'] == '') {
                    $itemModel = new WarehouseCommitDeliveryItem();
                    $itemModel->setAttributes($attributes);
                    $itemModel->link('owner', $this);
                } elseif (isset($item['id'])) {
                    $itemModel = WarehouseCommitDeliveryItem::findOne($item['id']);
                    if (!is_null($itemModel)) {
                        $itemModel->updateAttributes($attributes);
                    }
                }
            }
        }
    }

    public function setStateToCommit()
    {
        $db = \Yii::$app->db->beginTransaction();
        foreach ($this->getCommitDeliveryItems()->all() as $item) {
            if ($item->produce_number == 0 && $item->stock_number == 0) {
                $db->rollBack();
                return false;
            }
            $package = $item->productPackage;
            foreach ($package->packageItems as $packageItem) {
                $packageItem->productStock->updateCounters([
                   'number' => -($packageItem->need_number * $item->pieces),
                ]);
            }
            $combination = $item->productCombination;
            foreach ($combination->combinationItems as $combinationItem) {
                $combinationItem->productStock->updateCounters([
                    'number' => -($combinationItem->need_number * $item->produce_number),
                ]);
            }
            $item->productStock->updateCounters([
                'number' => -($item->stock_number)
            ]);
            $item->updateAttributes(['state' => WarehouseCommitState::COMMIT]);
        }
        $db->commit();
        $this->updateAttributes(['state' => WarehouseCommitState::COMMIT]);
        return true;
    }

}